/*
 * graph2D
 * Copyright (c) 2011 Shun Moriya <shun@mnu.sakura.ne.jp>
 *
 * The MIT License
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#if !defined(___PVRTC_LOADER_H___)
#define ___PVRTC_LOADER_H___

#include <OpenGLES/ES1/gl.h>
#include <OpenGLES/ES1/glext.h>

namespace Graph2D
{
	namespace Implementation
	{
		class PVRTCLoader
		{
		public:
			enum Constants
			{
				PVRTC_FLAG_TYPE_RGBA4444 = 16,
				PVRTC_FLAG_TYPE_RGBA1555 = 17,
				PVRTC_FLAG_TYPE_RGBA8888 = 18,
				PVRTC_FLAG_TYPE_RGB565 = 19,
				PVRTC_FLAG_TYPE_RGB888 = 21,
				PVRTC_FLAG_TYPE_I8 = 22,
				PVRTC_FLAG_TYPE_AI88 = 23,
				PVRTC_FLAG_TYPE_PVRTC_2 = 24,
				PVRTC_FLAG_TYPE_PVRTC_4 = 25,
				PVRTC_FLAG_TYPE_A8 = 27,
				PVRTC_FLAG_TYPE_MASK = 0xff,
				PVRTC_MAX_SURFACES = 16
			};

			struct Header
			{
				unsigned int headerSize;
				unsigned int height;
				unsigned int width;
				unsigned int numMipmaps;
				unsigned int flags;
				unsigned int dataSize;
				unsigned int bpp;
				unsigned int bitmaskRed;
				unsigned int bitmaskGreen;
				unsigned int bitmaskBlue;
				unsigned int bitmaskAlpha;
				unsigned int tag;
				unsigned int numSurfaces;
			};

			struct Surface
			{
				GLuint size;
				const void* bits;
			};

		private:
			GLuint width;
			GLuint height;
			GLuint name;
			GLenum format;
			bool alpha;
			Surface surfaces[PVRTC_MAX_SURFACES];
			GLuint numSurfaces;

			static const char PVRIdentifier[4];

		public:
			PVRTCLoader();
			PVRTCLoader(const char* path);
			~PVRTCLoader();

			bool loadFromFile(const char* path);
			bool loadFromMemory(const void* pData);

			GLuint getWidth() const
			{
				return width;
			}

			GLuint getHeight() const
			{
				return height;
			}

			GLenum getFormat() const
			{
				return format;
			}

			bool hasAlpha() const
			{
				return alpha;
			}

			GLuint getSurfaceCount() const
			{
				return numSurfaces;
			}

			GLuint getName() const
			{
				return name;
			}
		};
	}
}

#endif
